package game

import (
	"encoding/json"

	"gitee.com/shiwjlinux/arm/common"
)

// "game_name": "原神",
// "package_name": "com.miHoYo.ys.mi",
// "uid": 11902,
// "register_type": 1,
// "multi_sync": 0

func NewSyncGameSet() *SyncGameSet {
	return &SyncGameSet{
		GameItemInfo: []*CreateGameRequest{},
	}
}

func (s *SyncGameSet) String() string {
	return common.ToJsonString(s)
}

type SyncGameSet struct {
	GameItemInfo []*CreateGameRequest `json:"game_item_info"`
}

type CreateGameRequest struct {
	GameName     string `json:"game_name"`
	PackageName  string `json:"package_name" validate:"required"`
	Uid          int64  `json:"uid"`
	RegisterType int64  `json:"register_type"`
	MultiSync    int64  `json:"multi_sync"`
}

func NewCreateGameRequest() *CreateGameRequest {
	return &CreateGameRequest{}
}

type Game struct {
	*common.Meta
	*CreateGameRequest
	Tags             map[string]string `json:"tags" gorm:"serializer:json"`
	VersionName      string            `json:"versionName"`      // 版本号
	VersionCode      string            `json:"versionCode"`      // 版本号
	TimeStamp        string            `json:"timeStamp"`        // 时间戳
	FirstInstallTime string            `json:"firstInstallTime"` // 首次安装时间
	LastUpdateTime   string            `json:"lastUpdateTime"`   // 最后更新时间
	Summary          string            `json:"summary"`          // versionCode=226 minSdk=21 targetSdk=30
}

func NewGame() *Game {
	return &Game{
		Meta:              common.NewMeta(),
		CreateGameRequest: NewCreateGameRequest(),
		Tags:              map[string]string{},
	}
}

func (g *Game) TableName() string {
	return "game"
}

func (g *Game) String() string {
	return common.ToJsonString(g)
}

func (g *Game) GetGameName() string {
	if g.GameName != "" {
		return g.GameName
	}
	return ""
}

type GameSet struct {
	Games []*Game `json:"games"`
	Total int64   `json:"total"`
}

func NewGameSet() *GameSet {
	return &GameSet{
		Games: []*Game{},
		Total: 0,
	}
}

func (gs *GameSet) String() string {
	return common.ToJsonString(gs)
}

func (gs *GameSet) Parse(stdout string) error {
	return json.Unmarshal([]byte(stdout), &gs.Games)
}
